package deviceinfo.wsm.com.deviceinfo.hardware;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.CountDownTimer;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import deviceinfo.wsm.com.deviceinfo.WsmDeviceInfoSDK;

import static android.content.Context.SENSOR_SERVICE;

/**
 * Created by 王家宝 on 2018/7/6.
 * TEL:18241150668
 * MAIL:639586819@qq.com
 * 加速度计监控器
 */

public class AccelerometerWorker implements SensorEventListener {

    private SensorManager mSensorManager;
    private ArrayList<LinkedHashMap> acclerometerSensorResult = new ArrayList<>();
    private ArrayList<LinkedHashMap> gravitySensorResult = new ArrayList<>();
    private ArrayList<LinkedHashMap> gyroscopeSensorResult = new ArrayList<>();
    private boolean needSave = true;
    private int count1 = 0;
    private int count2 = 0;
    private int count3 = 0;


    AccelerometerWorker(final OnSensorDataBackListener onSensorDataBackListener) {
        try {
            mSensorManager = (SensorManager) WsmDeviceInfoSDK.getWsmSDK().
                    getAppContext().
                    getSystemService(SENSOR_SERVICE);

            final Sensor mSensorAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            final Sensor mSensorGravity = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
            final Sensor mSensorGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);


            mSensorManager.registerListener(this, mSensorAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
            mSensorManager.registerListener(this, mSensorGravity, SensorManager.SENSOR_DELAY_NORMAL);
            mSensorManager.registerListener(this, mSensorGyroscope, SensorManager.SENSOR_DELAY_NORMAL);

            CountDownTimer countDownTimer = new CountDownTimer(3200, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    needSave = true;
                }

                @Override
                public void onFinish() {
                    mSensorManager.unregisterListener(AccelerometerWorker.this, mSensorAccelerometer);
                    mSensorManager.unregisterListener(AccelerometerWorker.this, mSensorGravity);
                    mSensorManager.unregisterListener(AccelerometerWorker.this, mSensorGyroscope);
                    onSensorDataBackListener.onSensorDataBackSuccess(
                            acclerometerSensorResult,
                            gravitySensorResult,
                            gyroscopeSensorResult
                    );
                }
            }.start();
        } catch (Exception e) {
            onSensorDataBackListener.onSensorDataBackFailed();
        }


    }


    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {


        int type = sensorEvent.sensor.getType();

        if (needSave) {

            float xValue = sensorEvent.values[0];// Acceleration minus Gx on the x-axis}
            float yValue = sensorEvent.values[1];//Acceleration minus Gy on the y-axis
            float zValue = sensorEvent.values[2];//Acceleration minus Gz on the z-axis
//            Log.i("DEVICE_TAG", "x轴： " + xValue + "  y轴： " + yValue + "  z轴： " + zValue);

            LinkedHashMap<String, Float> sensorMap = new LinkedHashMap<>();
            sensorMap.put("x", xValue);
            sensorMap.put("y", yValue);
            sensorMap.put("z", zValue);

            switch (type) {
                case Sensor.TYPE_ACCELEROMETER:
                    if (count1 == 0) {
                        acclerometerSensorResult.add(sensorMap);
                        count1++;
                    }
                    break;
                case Sensor.TYPE_GRAVITY:
                    if (count2 == 0) {
                        gravitySensorResult.add(sensorMap);
                        count2++;
                    }
                    break;
                case Sensor.TYPE_GYROSCOPE:
                    if (count3 == 0) {
                        gyroscopeSensorResult.add(sensorMap);
                        count3++;
                    }
                    break;
            }

            if ((count1 + count2 + count3) == 3 && count1 == 1
                    && count2 == 1 && count3 == 1) {
                needSave = false;
                count1 = 0;
                count2 = 0;
                count3 = 0;
            }
//            needSave = false;
        }


    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
}
